/***
This do-file creates a binscatter at the ZCTA x Sector level on changes in small
business revenue vs rent, with sector fixed effects.
***/

*-------------------------------------------------------------------------------
* Set up
*-------------------------------------------------------------------------------

* Set $root 
project figstabs, root
if (r(buildrunning)==0) include "${root}/code/config_interactive.do"

* Set globals
project, uses("${root}/code/set_globals.do")
include "${root}/code/set_globals.do"

* Create required subfolders
cap mkdir "${root}/results/Small Business Revenue"
cap mkdir "${root}/results/paper numbers"

*-------------------------------------------------------------------------------
* Load and process data
*-------------------------------------------------------------------------------

project, uses("${root}/data/dvc/Womply/Womply - ZCTA - Weekly _unmasked.csv")
import delimited  "${root}/data/dvc/Womply/Womply - ZCTA - Weekly _unmasked.csv", clear

gen date = mdy(month, day_endofweek, year)
gen week = week(date)

reshape long revenue_ , i(zcta date week) j(sector) string

keep if year(date) == 2020 & inrange(week, 13, 15)
gcollapse (mean) revenue_ , by(zcta sector)

* Add ZCTA-level covariates
project, uses("${root}/data/derived/ACS 2014-2018 5-Year ZCTA/ACS 2014-2018 ZCTA.dta")
merge m:1 zcta using "${root}/data/derived/ACS 2014-2018 5-Year ZCTA/ACS 2014-2018 ZCTA.dta", keep(1 3) nogen keepusing(pop_2014_2018_est medhhinc_2014_2018_est med_2br_2014_2018_est zarealand)

rename pop_2014_2018_est pop_2018
rename med_2br_2014_2018_est med_2br_2018

replace revenue_ = revenue_ * 100 

* Winsorize changes 
foreach var in revenue_ { 
	gen wins_`var' = `var'
	sum `var' [w = pop_2018], d 
	replace wins_`var' = `r(p99)' if `var' >= `r(p99)' & !mi(`var')
}

* Merge counties (for SEs)
rename zcta zcta5
preserve
	project, uses("${root}/data/dvc/ACS 2014-2018 5-Year ZCTA/Area/zcta_county_rel_10.txt")
	import delimited "${root}/data/dvc/ACS 2014-2018 5-Year ZCTA/Area/zcta_county_rel_10.txt", encoding(ISO-8859-2) clear
	sort zcta5 poppt
	bysort zcta5 (poppt): keep if _n == _N // only keeps the county where most of the zcta lives
	gen county_fips = 1000 * state + county
	keep zcta5 county_fips
	tempfile zcta_county_CW
	save `zcta_county_CW'
restore

merge m:1 zcta5 using `zcta_county_CW', keep(1 3) nogen
rename zcta5 zcta 

reg wins_revenue_ med_2br_2018 [w = pop_2018], absorb(sector) 
		local beta = _b[med_2br_2018] * 1000
		local display_beta: display %4.2f `beta'
		local std_err = _se[med_2br_2018] * 1000
		local display_std_err: display %4.2f `std_err'

*-------------------------------------------------------------------------------
* Plot
*-------------------------------------------------------------------------------

binscatter wins_revenue_ med_2br_2018 ///
		[w = pop_2018], absorb(sector) ///
		xtitle("")  ///
		xlabel(400 "$400" 1000 "$1,000" 1600 "$1,600" 2200 "$2,200", nogrid) ///
		yscale(range(-63 -27)) ///
		xtitle("Median Two Bedroom Monthly Rent in 2014-2018 ($)")  ///
		ytitle("Change in Small Business Revenue (%)" "from January to April 2020") ///
		ylabel(-60 "-60%" -50 "-50%" -40 "-40%" -30 "-30%", nogrid) ///
		text(-63 400 "Slope = `display_beta'%`unit' (s.e. = `display_std_err')", place(ne) just(left) color(gs8) size(medlarge)) ///
		legend(off)

oi_graph_export "${root}/results/Small Business Revenue/Small Business Revenue vs Median Rent binscatter - Sector FEs", type(${fig_type})
